package com.pai4j.pgc.entity;

import com.pai4j.pgc.converter.StringListConverter;
import jakarta.persistence.*;
import lombok.Data;

import java.util.Date;
import java.util.List;

/**
 * 创作者实体类
 *
 * @author: CYM-pai
 * @date: 2025/11/01
 */
@Data
@Entity
@Table(name = "pai4j_creator", indexes = {
    @Index(name = "uk_user_id", columnList = "user_id", unique = true),
    @Index(name = "idx_status", columnList = "status"),
    @Index(name = "idx_level", columnList = "level"),
    @Index(name = "idx_experience_points", columnList = "experience_points")
})
public class CreatorEntity extends BaseEntity {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /**
     * 用户ID（关联用户模块）
     */
    @Column(name = "user_id", nullable = false, unique = true, length = 64)
    private String userId;
    
    /**
     * 创作者昵称
     */
    @Column(name = "creator_name", length = 128)
    private String creatorName;
    
    /**
     * 头像URL
     */
    @Column(name = "avatar", length = 512)
    private String avatar;
    
    /**
     * 个人简介
     */
    @Column(name = "bio", length = 500)
    private String bio;
    
    /**
     * 真实姓名
     */
    @Column(name = "real_name", length = 64)
    private String realName;
    
    /**
     * 身份证号（加密存储）
     */
    @Column(name = "id_card", length = 128)
    private String idCard;
    
    /**
     * 手机号
     */
    @Column(name = "phone", length = 32)
    private String phone;
    
    /**
     * 邮箱
     */
    @Column(name = "email", length = 128)
    private String email;
    
    /**
     * 擅长领域（JSON存储，使用Converter转换）
     */
    @Column(name = "expertise_fields", columnDefinition = "JSON")
    @Convert(converter = StringListConverter.class)
    private List<String> expertiseFields;
    
    /**
     * 认证材料URL列表（JSON存储）
     */
    @Column(name = "certification_materials", columnDefinition = "JSON")
    @Convert(converter = StringListConverter.class)
    private List<String> certificationMaterials;
    
    /**
     * 状态：0-待审核 1-已通过 2-已拒绝 3-已禁用
     */
    @Column(name = "status", nullable = false)
    private Integer status;
    
    /**
     * 审核时间
     */
    @Column(name = "audit_time")
    private Date auditTime;
    
    /**
     * 审核人
     */
    @Column(name = "audit_by", length = 64)
    private String auditBy;
    
    /**
     * 拒绝原因
     */
    @Column(name = "reject_reason", length = 500)
    private String rejectReason;
    
    /**
     * 创作者等级：1-新手 2-进阶 3-高级 4-专家 5-大师
     */
    @Column(name = "level", nullable = false)
    private Integer level;
    
    /**
     * 信用分（0-100）
     */
    @Column(name = "credit_score", nullable = false)
    private Integer creditScore;
    
    /**
     * 经验值
     */
    @Column(name = "experience_points", nullable = false)
    private Integer experiencePoints;
    
    /**
     * 粉丝数
     */
    @Column(name = "follower_count", nullable = false)
    private Integer followerCount;
    
    /**
     * 文章数
     */
    @Column(name = "article_count", nullable = false)
    private Integer articleCount;
    
    /**
     * 总阅读量
     */
    @Column(name = "total_views", nullable = false)
    private Long totalViews;
    
    /**
     * 总点赞数
     */
    @Column(name = "total_likes", nullable = false)
    private Long totalLikes;
    
    /**
     * 总收益（单位：分）
     */
    @Column(name = "total_income", nullable = false)
    private Long totalIncome;
    
    /**
     * 可提现余额（单位：分）
     */
    @Column(name = "available_balance", nullable = false)
    private Long availableBalance;
    
    /**
     * 冻结金额（单位：分）
     */
    @Column(name = "frozen_balance", nullable = false)
    private Long frozenBalance;
    
    /**
     * 删除标识：0-已删除 1-未删除
     */
    @Column(name = "delete_flag", nullable = false)
    private Integer deleteFlag;
    
    /**
     * 删除人
     */
    @Column(name = "delete_by", length = 64)
    private String deleteBy;
    
    /**
     * 删除时间
     */
    @Column(name = "delete_date")
    private Date deleteDate;
    
    @PrePersist
    public void prePersist() {
        super.prePersist();
        if (this.status == null) {
            this.status = 0; // 默认待审核
        }
        if (this.level == null) {
            this.level = 1; // 默认新手
        }
        if (this.creditScore == null) {
            this.creditScore = 80; // 默认信用分80
        }
        if (this.experiencePoints == null) {
            this.experiencePoints = 0;
        }
        if (this.followerCount == null) {
            this.followerCount = 0;
        }
        if (this.articleCount == null) {
            this.articleCount = 0;
        }
        if (this.totalViews == null) {
            this.totalViews = 0L;
        }
        if (this.totalLikes == null) {
            this.totalLikes = 0L;
        }
        if (this.totalIncome == null) {
            this.totalIncome = 0L;
        }
        if (this.availableBalance == null) {
            this.availableBalance = 0L;
        }
        if (this.frozenBalance == null) {
            this.frozenBalance = 0L;
        }
        if (this.deleteFlag == null) {
            this.deleteFlag = 1; // 默认未删除
        }
    }
}




